import { loginApi, UserInfoApi } from '@/api/user'

import { setToken, getToken } from '@/utils/auth'
export default {
  namespaced: true,
  state() {
    return {
      token: '' || getToken(),
      userinfo: {}
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setUserInfo(state, userinfo) {
      state.userinfo = userinfo
    }
  },
  actions: {
    // 异步登录
    async asyncLogin(ctx, form) {
      const token = await loginApi(form)
      console.log(token)
      ctx.commit('setToken', token)
    },
    async asyncGetUserInfo(ctx) {
      const res = await UserInfoApi()
      // console.log(res)
      ctx.commit('setUserInfo', res)
    },
    asyncLogOut(ctx) {
      ctx.commit('setToken', '')
      ctx.commit('setUserInfo', {})
    }
  }
}
